import { createSlice } from "@reduxjs/toolkit";

const counterStore = createSlice({
  name: "counter",
  // 初始化 state
  initialState: {
    count: 10,
  },
  // 同步方法，直接修改 state 中的属性值，支持直接修改，不需要 setter
  reducers: {
    increment(state) {
      state.count++;
    },
    decrement(state) {
      state.count--;
    },
    incrOffset(state, action) {
      state.count += action.payload;
    },
    decrOffset(state, action) {
      state.count -= action.payload;
    },
  },
});

// 结构出来 actionCreater 函数
const { increment, decrement, incrOffset, decrOffset } = counterStore.actions;

// 获取 reducer
const reducer = counterStore.reducer;

// 按需导出 actionCreater
export { increment, decrement, incrOffset, decrOffset };

// 默认导出 reducer
export default reducer;
